/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.jpa;

import java.util.Collection;
import java.util.List;

import javax.persistence.LockModeType;

import org.openinsula.vulcano.orm.dao.Dao;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.springframework.orm.jpa.support.JpaDaoSupport;

/**
 * @author Eduardo R Danielli
 * TODO
 */
//@Repository
public class JpaDaoImpl extends JpaDaoSupport implements Dao {

	@Override
	public void clear() {
		getJpaTemplate().getEntityManager().clear();
	}

	@Override
	public void delete(final Object entity) {
		getJpaTemplate().remove(entity);
	}

	@Override
	public void deleteAll(final Collection<?> entities) {
		for (Object entity : entities) {
			getJpaTemplate().remove(entity);
		}
	}

	@Override
	public <T> T findById(final Class<T> entityClass, final Object id) {
		return getJpaTemplate().find(entityClass, id);
	}

	@Override
	public void flush() {
		getJpaTemplate().flush();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T findUnique(final String queryString, final Object... values) {
		List<?> result = find(queryString, values);
		
		if (result.size() != 1) {
			throw new IllegalStateException("Query did not return unique result: " + result.size());
		}
		
		return (T) result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> find(final String queryString, final Object... values) {
		return getJpaTemplate().find(queryString, values);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> listAll(final Class<T> type) {
		String queryString = String.format("select k from %s k", type.getName());
		
		return getJpaTemplate().find(queryString);
	}

	@Override
	public void reattach(final Object entity) {
		getJpaTemplate().getEntityManager().lock(entity, LockModeType.READ);
	}

	@Override
	public void refresh(final Object entity) {
		getJpaTemplate().refresh(entity);
	}

	@Override
	public void replicate(final Object entity) {
		// TODO
	}

	@Override
	public void save(final Object entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void saveAll(final Collection<?> entities) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int deleteAll(final Class<?> entityClass) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(final String queryString, final Object... values) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public <T> List<T> find(final DaoQuery daoQuery) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T findUnique(final DaoQuery daoQuery) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void evict(final Object entity) {
		// TODO Auto-generated method stub
	}

	@Override
	public <T> T merge(final T entity) {
		return getJpaTemplate().merge(entity);
	}

}
